package com.example.administrator.personalmail.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.tu.loadingdialog.LoadingDailog;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.api.Contants;
import com.example.administrator.personalmail.model.Wares;
import com.example.administrator.personalmail.widget.CartProvider;

import java.io.Serializable;

import cn.sharesdk.onekeyshare.OnekeyShare;

public class WebDetailActivity extends AppCompatActivity {
    private WebView mWebView;
    private CartProvider cartProvider;
    private Wares mWare;
    private WebAppInterface mAppInterfce;
    LoadingDailog dialog;
    private ImageView ivBack;
    private TextView tvShare;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ware_detail);
        mWebView = findViewById(R.id.wv_detail);
        ivBack = findViewById(R.id.iv_back_detail_last);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WebDetailActivity.this.finish();
            }
        });
        tvShare = findViewById(R.id.toolbar_share_detail);
        tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showShare();
            }
        });

        initData();

        initWebView();
    }

    private void showShare() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，微信、QQ和QQ空间等平台使用
        oks.setTitle(getString(R.string.share));
        // titleUrl QQ和QQ空间跳转链接
        oks.setTitleUrl(Contants.API.WARES_DETAIL);
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImageUrl(mWare.getImgUrl());//确保SDcard下面存在此张图片
        // url在微信、微博，Facebook等平台中使用
        oks.setUrl(Contants.API.WARES_DETAIL);
        // comment是我对这条分享的评论，仅在人人网使用
        oks.setComment("我是测试评论文本");
        // 启动分享GUI
        oks.show(this);
    }

    private void initData(){
        Serializable serializable = getIntent().getSerializableExtra("wares");
        if (serializable == null){
            WebDetailActivity.this.finish();
        }
        mWare = (Wares) serializable;
        cartProvider = new CartProvider(this);
        showDialog();
    }

    private void initWebView(){

        WebSettings settings = mWebView.getSettings();

        settings.setJavaScriptEnabled(true);
        settings.setBlockNetworkImage(false);
        settings.setAppCacheEnabled(true);


        mWebView.loadUrl(Contants.API.WARES_DETAIL);

        mAppInterfce = new WebAppInterface(this);
        mWebView.addJavascriptInterface(mAppInterfce,"appInterface");
        mWebView.setWebViewClient(new WC());
    }

    public void showDialog(){
        LoadingDailog.Builder loadBuilder=new LoadingDailog.Builder(this)
                .setMessage("加载中...")
                .setCancelable(true)
                .setCancelOutside(true);
        dialog=loadBuilder.create();
        dialog.show();
    }

    class  WC extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
//            if(mDialog !=null && mDialog.isShowing())
//                mDialog.dismiss();
            mAppInterfce.showDetail();
            dialog.dismiss();
        }
    }

    class WebAppInterface{

        private Context mContext;
        public WebAppInterface(WebDetailActivity context){
            mContext = context;
        }

        @JavascriptInterface
        public  void showDetail(){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mWebView.loadUrl("javascript:showDetail("+mWare.getId()+")");
                }
            });
        }

        @JavascriptInterface
        public void buy(long id){
            Intent intent = new Intent(WebDetailActivity.this,CreateOrderActivity.class);
            intent.putExtra("sum",mWare.getPrice()+"");
            startActivity(intent);
        }

        @JavascriptInterface
        public void addToCart(long id){
            cartProvider.put(mWare);
            Toast.makeText(mContext,"已添加到购物车",Toast.LENGTH_SHORT).show();
        }

        @JavascriptInterface
        public void addFavorites(long id){
//            addToFavorite();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }
}
